#!/usr/bin/env bash

if [ "$1" == "--help" ]; then
  echo "$(basename $0) <optional list of tests in form file:func>"
  echo "e.g.: "
  echo " - run all tests: $0"
  echo " - run specific tests script: $0 signature_help.test.vim"
  echo " - run specific tests fun: $0 signature_help.test.vim:Test_signatures_TopLine\(\)"
  exit 0
elif [ "$1" == "--stdout" ]; then
  export YCM_TEST_STDOUT=1
  shift
fi

RUN_VIM="vim --clean --not-a-term"
RUN_TEST="${RUN_VIM} -S lib/run_test.vim"

pushd $(dirname $0) > /dev/null

echo "Running YouCompleteMe Vim tests"

RESULT=0

TESTS="$@"

if [ -z "$TESTS" ]; then
  TESTS=*.test.vim
fi

for t in ${TESTS}; do
  echo ""
  echo "%RUN: $t"

  # split on : into fileName and testName
  IFS=: read -s t T <<< "$t"

  TESTLOGDIR=$(pwd)/logs/$t

  if ${RUN_TEST} --cmd 'au SwapExists * let v:swapchoice = "e"' $t $T \
     && [ -f $t.res ];  then
    echo "%PASS: $t PASSED"
  else
    echo "%FAIL: $t FAILED - see $TESTLOGDIR"
    RESULT=1
  fi

  rm -rf $TESTLOGDIR
  mkdir -p $TESTLOGDIR
  ${RUN_VIM} --version > ${TESTLOGDIR}/vimversion
  for l in messages debuglog test.log *.testlog; do
    # In CI we can't view the output files, so we just have to cat them
    if [ -f $l ]; then
      if [ "$YCM_TEST_STDOUT" ]; then
        echo ""
        echo ""
        echo "*** START: $l ***"
        cat $l
        echo "*** END: $l ***"
      fi
      mv $l $TESTLOGDIR
    fi
  done

  if [ -n "${COVERAGE}" ]; then
    covimerage write_coverage --append \
                              --source $(pwd)/.. \
                              --data-file .coverage.vim \
                              .vim_profile
  fi

  rm -f $t.res
done

echo "Done running tests"

if [ -n "${COVERAGE}" ]; then
  mv .coverage.* ../
fi

popd > /dev/null

echo ""
echo "All done."


exit $RESULT
